﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    /*    */
    [NoCache]
    public class CommonController : Controller
    {
        public EdexApiClient.EdexApiContext EdexContext
        {
            get
            {
                if (Session["api_session"] == null)
                    throw new Exception("Нет доступа к данным");

                return new EdexApiClient.EdexApiContext(((EdexApiClient.API_SESSION)Session["api_session"]).APIKEY);
            }
        }
    }

    [NoCache]
    public class AsyncCommonController : AsyncController
    {
        public EdexApiClient.EdexApiContext EdexContext
        {
            get
            {
                if (Session["api_session"] == null)
                    throw new Exception("Нет доступа к данным");

                return new EdexApiClient.EdexApiContext(((EdexApiClient.API_SESSION)Session["api_session"]).APIKEY);
            }
        }
    }


    [NoCache]
    public class AsyncCommonControllerVersion2 : AsyncController
    {
        public EdexApiClient.api.v2.EdexApiContext EdexContext
        {
            get
            {
                if (Session["api_key"] == null)
                    throw new Exception("Нет доступа к данным");

                return new EdexApiClient.api.v2.EdexApiContext((string)Session["api_key"]);
            }
        }

        public string api_key
        {
            get
            {
                if (Session["api_key"] == null)
                    throw new Exception("Нет доступа к данным");

                return (string)Session["api_key"];
            }
        }
    }

    [AttributeUsage(AttributeTargets.Class)]
    public sealed class NoCacheAttribute : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
            filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            filterContext.HttpContext.Response.Cache.SetNoStore();

            base.OnResultExecuting(filterContext);
        }
    }
}
